/**
 * Created by ivancai on 2017/7/24.
 */

//提示框
function TipLayer (sCont,sStyle,fn,T) {
    layer.open({
        content: sCont,
        style:sStyle != 'no' ? sStyle : 'background-color:rgba(255,255,255,1); color:#252525; border:none; border-radius: 5px;',
        scrollbar: false,
        time: T || 2,
        shadeClose: true,
        success:function() {if(fn)fn();}
    });
}
var minCount=$('#minCount').val()*1;
if(minCount==null || minCount=="undefined"){
    minCount=1;
}
// 最高限售份数 maxCount为0时是不限购
var limitSize = 99;
var maxCount = $('#maxCount').val()*1;
if(maxCount != null || maxCount != "undefined"){
    if(maxCount != 0 && maxCount <= limitSize){
        limitSize = maxCount
    }else{
        limitSize = 99
    }
}

// 税费
var taxRare = 0;
var taxCount = 0;
// 计算初始化的显示税费
if($('#taxPrize_tg') && $('#taxPrize_tg').length>0){
    taxRare = $('#taxPrize_tg').parents('.js_taxRareTg').siblings('.top_div2').find('li.on').attr('taxFare');
    if(taxRare && taxRare!= undefined && taxRare != ""){
        taxCount = (taxRare*minCount).toFixed(2)*1;
    }else{
        taxCount = 0;
    }
    $('#taxPrize_tg').text(taxCount)
}

var   buyCountTg =minCount, curPriceTg = $('#price_tg').text(), curProductIdTg = $('#proId_tg').val(),originPriceTg = curPriceTg,curStoreTg=$('#store_tg').text(),memberScore=$("#costMemberScore").text()
// setDefaultUITg()
function onChooseItemTg(productId,name, store, price, imgPath,costMemberScore,awardMemberScore, taxFareLi, li) {
    originPriceTg = curPriceTg = price, buyCountTg = minCount ,curProductIdTg = productId,curStoreTg=store,memberScore=costMemberScore
    if(taxFareLi && taxFareLi != '' && taxFareLi != undefined){
        taxRare = taxFareLi*1;
        taxCount = (taxFareLi*buyCountTg).toFixed(2)*1;
    }else{
        taxRare = 0;
        taxCount = 0
    }
    price = price*1;
    curPriceTg = curPriceTg*1;
    if($(li).hasClass('noStore')) {
        TipLayer("该商品已经没有库存啦~");
        return;
    }
    updateBuyCountUITg()
    changeCurStatusTg(name, store, price, taxCount, imgPath)
    $('#costMemberScore').text(costMemberScore)
    $('#awardMemberScore').text(awardMemberScore)
    $('.productItem2').removeClass("on")
    $(li).addClass("on")
}
function changeCurStatusTg(name, store, price, taxPrize, imgPath) {
    /*console.log("name:"+name)
    console.log("store:"+store)
    console.log("price:"+price)
    console.log("imgPath:"+imgPath)*/
    $('#proName_tg').text(name)
    $('#store_tg').text(store)
    $('#price_tg').text(price)
    $('#taxPrize_tg').text(taxPrize);
    if( imgPath != ''){
        $('#proImage_tg').attr("src", imgPath);
    }

}
function setDefaultUITg() {
    $($('.productItem2')[0]).addClass("on")
}
function onPlusTg() {
    if (buyCountTg < limitSize) {
        if(buyCountTg>=curStoreTg){
            TipLayer("选购数量不能超过库存哦~");
            return
        }
        buyCountTg++
        taxCount = (buyCountTg * taxRare).toFixed(2)*1;
        curPriceTg =( buyCountTg * originPriceTg).toFixed(2)*1;
        $('#costMemberScore').text(buyCountTg * memberScore)
        updateBuyCountUITg()
    }else{
        TipLayer("最多只能"+limitSize+"，不要太贪心哦");
    }
}
function onReduceTg() {
    if (buyCountTg <= minCount){
        TipLayer("不能再少了~");
        return
    }
    buyCountTg--
    taxCount = (buyCountTg * taxRare).toFixed(2)*1;
    curPriceTg = (buyCountTg * originPriceTg).toFixed(2)*1;
    $('#costMemberScore').text(buyCountTg * memberScore)
    updateBuyCountUITg()
}
function updateBuyCountUITg() {
    $('#buyCount_tg').val(buyCountTg);
    $('#price_tg').text(curPriceTg);
    $('#taxPrize_tg').text(taxCount)
}
$("#buyCount_tg").bind('input porpertychange', function () {
    var input = $("#buyCount_tg").val()*1;
    // 库存与最小限制作比较
    if(parseInt(curStoreTg)<minCount){
        TipLayer("库存不足~");
    }else{
        if (input <= minCount) {
            TipLayer("不能再少了~");
            $("#buyCount_tg").val(minCount)
        }
    }

    // 库存与最大限制作比较
    if(parseInt(curStoreTg)>=limitSize){
        if(input >= limitSize){
            TipLayer("最多只能"+limitSize+"，不要太贪心哦");
            $("#buyCount_tg").val(limitSize)
        }
    }else{
        if(input >= parseInt(curStoreTg)){
            TipLayer("选购数量不能超过库存哦~");
            $("#buyCount_tg").val(curStoreTg);
            input = $("#buyCount_tg").val();
            buyCountTg = input;
            taxCount = (buyCountTg * taxRare).toFixed(2)*1;
             curPriceTg = (buyCountTg * originPriceTg).toFixed(2)*1;
            updateBuyCountUITg();
            return
        }
    }
    input = $("#buyCount_tg").val();
    buyCountTg = input;
    taxCount = (buyCountTg * taxRare).toFixed(2)*1;
    curPriceTg = (buyCountTg * originPriceTg).toFixed(2)*1;
    updateBuyCountUITg()
});
function submitTuangouOrder() {
    if ( $('.js_productTgList').find('li.on').length == 0 ) {
        TipLayer("请选择商品规格！");
        return;
    }else{
        var activityId = $('#activityId').val()
        var productAndquantity=curProductIdTg+','+buyCountTg
        window.location.href='/shop/pay/buynow?productIdAndquantity='+productAndquantity+"&isGroupBuy=yes&groupBuyType=GroupBuyTypeOpen&groupBuyActivityId="+activityId
    }
}